package com.woniuxy.charin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.charin.mapper.ActivityInfoMapper;
import com.woniuxy.charin.mapper.ActivityStationMapper;
import com.woniuxy.charin.model.entity.ActivityInfo;
import com.woniuxy.charin.model.entity.ActivityStation;
import com.woniuxy.charin.model.entity.Station;
import com.woniuxy.charin.model.vo.ActivityInfoVo;
import com.woniuxy.charin.service.ActivityInfoService;
import com.woniuxy.charin.service.StationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
/**
* @author 28196
* @description 针对表【activity_info】的数据库操作Service实现
* @createDate 2024-12-23 21:27:49
*/
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo>
    implements ActivityInfoService{

    @Autowired
    private StationService stationService;






    @Override
    public Page<ActivityInfoVo> convertToVOPage(Page<ActivityInfo> page) {
        Page<ActivityInfoVo> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<ActivityInfoVo> voList = page.getRecords().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }

    private ActivityInfoVo convertToVo(ActivityInfo activityInfo) {
        ActivityInfoVo vo = new ActivityInfoVo();
        BeanUtils.copyProperties(activityInfo, vo);

        // 获取关联的站点列表
        List<Station> stations = getActivityStations(activityInfo.getId());
        if (stations != null && !stations.isEmpty()) {
            // 这里可以根据需求处理多个站点的情况
            // 例如：将站点名称拼接起来
            String stationNames = stations.stream()
                    .map(Station::getStationName)
                    .collect(Collectors.joining(", "));
            vo.setStationName(stationNames);

            // 如果需要地址，可以类似处理
            String addresses = stations.stream()
                    .map(Station::getAddress)
                    .collect(Collectors.joining("; "));
            vo.setStationAddress(addresses);
        }

        return vo;
    }
    @Override
    public List<Station> getActivityStations(Long activityId) {
        ActivityInfo activity = this.getById(activityId);
        if (activity == null || StringUtils.isBlank(activity.getApplicableScope())) {
            return Collections.emptyList();
        }

        List<Long> stationIds = Arrays.stream(activity.getApplicableScope().split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());

        return stationService.listByIds(stationIds);
    }

    @Override
    @Transactional
    public boolean addActivityStations(Long activityId, List<Long> stationIds) {
        if (activityId == null || stationIds == null) {
            return false;
        }

        ActivityInfo activity = this.getById(activityId);
        if (activity == null) {
            return false;
        }

        // 更新活动的适用范围
        activity.setApplicableScope(String.join(",", stationIds.stream()
                .map(String::valueOf)
                .collect(Collectors.toList())));

        return this.updateById(activity);
    }

}




